export default class getDate {
    constructor() { }

    // 时间戳改时间 yyyy-mm-dd
    timestampToTime (value, l = '-', m = '-', n = '') {
        let d = new Date(Number(value))
        let year = d.getFullYear()
        let month = d.getMonth() + 1
        let day = d.getDate()
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        return year + l + month + m + day + n
    }

    // 时间戳改时间 yyyy-mm-dd hh:mm:ss
    timestampToTimes (value, l = "-", m = ":") {
        let d = new Date(value);
        let year = d.getFullYear()  // 年
        let month = d.getMonth() + 1  // 月
        let day = d.getDate()  // 日
        let hour = d.getHours()  // 时
        let minute = d.getMinutes()  // 分
        let second = d.getSeconds()  // 秒
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minute < 10) {
            minute = "0" + minute;
        }
        if (second < 10) {
            second = "0" + second;
        }
        return year + l + month + l + day + " " + hour + m + minute + m + second;
    }

    // 获取上周一的日期  yyyy-mm-dd
    getLastWeekMonday () {
        let d = new Date()
        let timestamp = d.getTime()  // 当前时间戳
        let day = d.getDay()  // 当前周几
        let thisMonday = timestamp - 24 * 60 * 60 * 1000 * (day - 1)  // 本周一时间戳
        let lastMonday = thisMonday - 24 * 60 * 60 * 1000 * 7
        return this.timestampToTime(lastMonday)
    }

    // 获取上周日的日期  yyyy-mm-dd
    getLastWeekSunday () {
        let d = new Date()
        let timestamp = d.getTime()  // 当前时间戳
        let day = d.getDay()  // 当前周几
        let thisMonday = timestamp - 24 * 60 * 60 * 1000 * (day - 1)  // 本周一时间戳
        let lastSunday = thisMonday - 24 * 60 * 60 * 1000 * 1
        return this.timestampToTime(lastSunday)
    }
}
